package com.newtype.meeting.client.widget.frontend;

import java.util.HashMap;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Time;
import com.extjs.gxt.ui.client.widget.form.TimeField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.Meeting;

/**
 * 修改会议内容面板
 * @author 汪恭泽
 *
 */
public class EditMeetingContentPanel extends ContentPanel {
	private FormPanel formPanel;
	private FormData formData;
	private BeanModel meetingBeanModel;

	private RadioGroup openType;//公开对象层级（0:全公司，2:自订对象）
	private HiddenField<String> openID;//公开对象ID
	private TextField<String> openName;//公开对象Name
	private HiddenField<String> deptID;//发起人部门ID
	private TextField<String> deptName;//发起人部门Name
	private HiddenField<String> originatorID;//发起人ID
	private TextField<String> originatorName;//发起人Name
	private HiddenField<String> invitedID;//与会人员ID
	private TextArea invitedName;//与会人员Name
	private HiddenField<String> invitedOthersID;//列席人员ID
	private TextField<String> invitedOthersName;//列席人员ID
	private HtmlEditor content;//议程
	private DateField date;//会议日期
	private TimeField startTime;//开始时间
	private TimeField endTime;//结束时间

	public EditMeetingContentPanel(Meeting meeting) {
		setHeading("修改会议内容");
		setLayout(new FormLayout());
		setScrollMode(Scroll.AUTO);

		//存放表单面板
		createFromPanel(meeting);
	}

	//存放表单面板
	private void createFromPanel(Meeting meeting) {
		formData = new FormData("95%");
		formPanel = new FormPanel();
		formPanel.setFrame(true);
		formPanel.setHeaderVisible(false);
		formPanel.setStyleAttribute("margin", "10px 100px 10px 100px");
		formPanel.setButtonAlign(HorizontalAlignment.CENTER);

		formPanel.add(createTitle("修改会议内容"));

		//会议公开对象ID
		openID = new HiddenField<String>();
		openID.setFieldLabel("公开对象ID");
		openID.setName("openID");
		openID.setValue(meeting.getOpenID());
		formPanel.add(openID);

		//会议公开对象Name
		openName = new TextField<String>();
		openName
				.setFieldLabel("<span style='color:#FFCC00;font-weight:bold;'>请选择对象</span>");
		openName.setName("openName");
		openName.setValue(meeting.getOpenName());
		openName.hide();
		openName.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				Map<String, Field<String>> organization = new HashMap<String, Field<String>>();
				organization.put("IDStr", openID);
				organization.put("nameStr", openName);
				Dispatcher.forwardEvent(AppEvents.ToOrganizationSeperateWindow,
						organization);
			}
		});

		//会议公开对象单选按钮
		Radio radio1 = new Radio();
		radio1.setBoxLabel("全公司");
		radio1.setValueAttribute("0");
		radio1.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				openName.hide();
			}
		});

		Radio radio2 = new Radio();
		radio2.setBoxLabel("自订对象");
		radio2.setValueAttribute("2");
		radio2.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				openName.show();
			}
		});

		openType = new RadioGroup();
		openType.setFieldLabel("<span class='span-field'>会议公开层级</span>");
		openType.setStyleAttribute("background", "#FFFFC2");
		openType.add(radio1);
		openType.add(radio2);
		//判断会议公开对象（0:全公司，2:自订对象）
		if (meeting.getOpenType() == "2" || meeting.getOpenType().equals("2")) {
			openType.setValue(radio2);
			openName.show();
		} else {
			openType.setValue(radio1);
		}
		formPanel.add(openType, formData);

		//添加会议公开对象Name
		formPanel.add(openName, formData);

		//发起人部门ID
		deptID = new HiddenField<String>();
		deptID.setFieldLabel("部门ID");
		deptID.setName("deptID");
		formPanel.add(deptID);

		//发起人部门Name
		deptName = new TextField<String>();
		deptName.setFieldLabel("<span class='span-field'>部门</span>");
		deptName.setName("deptName");
		deptName.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				Map<String, Field<String>> department = new HashMap<String, Field<String>>();
				department.put("pDeptID", deptID);
				department.put("pDeptName", deptName);
				Dispatcher.forwardEvent(AppEvents.ToDeptTreeSingleWindow,
						department);
			}
		});
		formPanel.add(deptName, formData);

		//发起人ID
		originatorID = new HiddenField<String>();
		originatorID.setFieldLabel("发起人ID");
		originatorID.setName("originatorID");
		formPanel.add(originatorID);

		//发起人Name
		originatorName = new TextField<String>();
		originatorName.setFieldLabel("<span class='span-field'>发起人</span>");
		originatorName.setName("originatorName");
		originatorName.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				Dispatcher.forwardEvent(AppEvents.ToPersonnelSingleWindow);
			}
		});
		formPanel.add(originatorName, formData);

		//主题
		TextField<String> subject = new TextField<String>();
		subject.setFieldLabel("<span class='span-field'>主题</span>");
		subject.setName("subject");
		formPanel.add(subject, formData);

		//会议主席
		TextField<String> chairName = new TextField<String>();
		chairName.setFieldLabel("<b>会议主席</b>");
		chairName.setName("chairName");
		formPanel.add(chairName, formData);

		//会议记录
		TextField<String> recorderName = new TextField<String>();
		recorderName.setFieldLabel("<b>会议记录</b>");
		recorderName.setName("recorderName");
		formPanel.add(recorderName, formData);

		//与会人员ID
		invitedID = new HiddenField<String>();
		invitedID.setFieldLabel("与会人员ID");
		invitedID.setName("invitedID");
		formPanel.add(invitedID);

		//与会人员Name
		invitedName = new TextArea();
		invitedName.setFieldLabel("<span class='span-field'>与会人员</span>");
		invitedName.setName("invitedName");
		invitedName.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				Map<String, Field<String>> personnel = new HashMap<String, Field<String>>();
				personnel.put("accountID", invitedID);
				personnel.put("fullName", invitedName);
				Dispatcher.forwardEvent(AppEvents.ToOrganizationWindow,
						personnel);
			}
		});
		formPanel.add(invitedName, formData);

		//列席人员ID
		invitedOthersID = new HiddenField<String>();
		invitedOthersID.setFieldLabel("列席人员ID");
		invitedOthersID.setName("invitedOthersID");
		formPanel.add(invitedOthersID);

		//列席人员Name
		invitedOthersName = new TextField<String>();
		invitedOthersName.setFieldLabel("<span class='span-field'>列席人员</span>");
		invitedOthersName.setName("invitedOthersName");
		invitedOthersName.addListener(Events.OnClick,
				new Listener<BaseEvent>() {
					@Override
					public void handleEvent(BaseEvent be) {
						Map<String, Field<String>> personnel = new HashMap<String, Field<String>>();
						personnel.put("accountID", invitedOthersID);
						personnel.put("fullName", invitedOthersName);
						Dispatcher.forwardEvent(AppEvents.ToOrganizationWindow,
								personnel);
					}
				});
		formPanel.add(invitedOthersName, formData);

		//外部指导人员
		TextArea guidance = new TextArea();
		guidance.setFieldLabel("<span class='span-field'>外部指导人员</span>");
		guidance.setName("guidance");
		guidance.setHeight(40);
		formPanel.add(guidance, formData);

		//议程
		content = new HtmlEditor();
		content.setFieldLabel("<span class='span-field'>议程</span>");
		content.setName("content");
		formPanel.add(content, formData);

		//会议日期
		date = new DateField();
		date.setFieldLabel("<span class='span-field'>会议日期</span>");
		date.setName("startDate");
		date.setWidth(100);
		date.setEditable(false);
		formPanel.add(date);

		//会议开始时间
		startTime = new TimeField();
		startTime.setFieldLabel("<span class='span-field'>开始时间</span>");
		startTime.setFormat(DateTimeFormat.getFormat("HH:mm"));
		startTime.setIncrement(10);
		Time time = new Time();
		time.setText(meeting.getStartTime());
		startTime.setValue(time);
		startTime.setTriggerAction(TriggerAction.ALL);
		startTime.setWidth(100);
		formPanel.add(startTime);

		//会议结束时间
		endTime = new TimeField();
		endTime.setFieldLabel("<span class='span-field'>结束时间</span>");
		endTime.setFormat(DateTimeFormat.getFormat("HH:mm"));
		endTime.setIncrement(10);
		time = new Time();
		time.setText(meeting.getEndTime());
		endTime.setValue(time);
		endTime.setTriggerAction(TriggerAction.ALL);
		endTime.setWidth(100);
		formPanel.add(endTime);

		//会议地点
		TextField<String> location = new TextField<String>();
		location.setFieldLabel("<span class='span-field'>会议地点</span>");
		location.setName("location");
		formPanel.add(location);

		//绑定值
		meetingBeanModel = BeanModelLookup.get().getFactory(Meeting.class)
				.createModel(meeting);
		FormBinding formBinding = new FormBinding(formPanel, true);
		formBinding.bind(meetingBeanModel);

		//添加提交/取消按钮
		formPanel.addButton(getSubmitButton());
		formPanel.addButton(getCancelButton(meeting));
		add(formPanel);
	}

	/**
	 * 送出按钮
	 * @return
	 */
	private Button getSubmitButton() {
		Button button = new Button("送出，并发送会议通知");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {

			}
		});
		return button;
	}

	/**
	 * 取消按钮
	 * @return
	 */
	private Button getCancelButton(final Meeting meeting) {
		Button button = new Button("取消");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToMeetingContent, meeting
						.getId());
			}
		});
		return button;
	}

	private Text createTitle(String str) {
		StringBuffer sb = new StringBuffer();
		sb.append("<div style='line-height: 2em;'>");
		sb.append("<p style='font-size:22px;font-weight: bold;color:#0060A0;'>"
				+ str + "</p>");
		sb
				.append("<span style='background-color:#CE0000; color:white;font-size:12px; padding:1px 5px 1px 5px;'>修改会议内容会重新发送会议通知及预订场地</span>");
		sb.append("<hr size='1'>");
		sb.append("</div>");
		Text title = new Text(sb.toString());
		return title;
	}

}